package com.ghf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.ghf.data.GDayWeather;
import com.ghf.data.GGoogleWeather;

import android.util.Log;
import android.util.Xml;

public class GNet {

	public GGoogleWeather getWeather(String city) throws Exception{
		URL url;
		GGoogleWeather rs = null;
		StringBuffer resultData = new java.lang.StringBuffer();
		try {
			Log.d(GWeatherActivity.ACTIVITY_TAG,"开始建立连接");
			url = new URL("http://www.google.com/ig/api?weather=" + city);
			HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
			urlConn.setUseCaches(false); 
			rs = this.xml(urlConn.getInputStream());
			Log.d(GWeatherActivity.ACTIVITY_TAG,rs.getDay1().getWeek());
			Log.d(GWeatherActivity.ACTIVITY_TAG,"数据传输完成连接");
             /*/得到读取的内容(流)  
             InputStreamReader in = new InputStreamReader(urlConn.getInputStream());  
             // 为输出创建BufferedReader  
             BufferedReader buffer = new BufferedReader(in);  
             String inputLine = null;  
             //使用循环来读取获得的数据  
             while (((inputLine = buffer.readLine()) != null))  
             {  
                 //我们在每一行后面加上一个"\n"来换行  
            	 resultData.append(inputLine);
             }           
             //关闭InputStreamReader  
             in.close();  
             Log.d(GWeatherActivity.ACTIVITY_TAG,new String(resultData));
             //关闭http连接  */
             urlConn.disconnect();              
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			Log.d(GWeatherActivity.ACTIVITY_TAG,e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d(GWeatherActivity.ACTIVITY_TAG,e.getMessage());
			e.printStackTrace();
		}  
		return rs;
	}
	
	public GGoogleWeather xml(InputStream inStream) throws Exception{
		Log.d(GWeatherActivity.ACTIVITY_TAG,"开始解析数据");
		GGoogleWeather gweather = new GGoogleWeather();
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();
		int index = 1;
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if(eventType == XmlPullParser.START_DOCUMENT){
				
			}else if(eventType == XmlPullParser.START_TAG){
				String name = parser.getName();
				if(name.equalsIgnoreCase("postal_code")){
					gweather.setCity(parser.getAttributeValue(null,"data"));
				}else if(name.equalsIgnoreCase("current_conditions")){
					//当天的信息
					GDayWeather curday = new GDayWeather();
					eventType = parser.next();
					curday.setDesc(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					eventType = parser.next();
					curday.setCursor(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setHumidity(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setIoc(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setWind(parser.getAttributeValue(null,"data"));
					gweather.setCursorday(curday);
				}else if(name.equalsIgnoreCase("forecast_conditions")){
					GDayWeather curday = new GDayWeather();
					eventType = parser.next();
					curday.setWeek(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setLow(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setHigh(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setIoc(parser.getAttributeValue(null,"data"));
					eventType = parser.next();
					curday.setDesc(parser.getAttributeValue(null,"data"));
					switch(index){
						case 1:
							gweather.setDay1(curday);
							index++;
							break;
						case 2:
							gweather.setDay2(curday);
							index++;
							break;
						case 3 :
							gweather.setDay3(curday);
							index++;
							break;
						case 4:
							gweather.setDay4(curday);
							index++;
							break;						
					}
				}
			}else if(eventType == XmlPullParser.END_TAG){
			}
			eventType = parser.next();
		}
		inStream.close();
		Log.d(GWeatherActivity.ACTIVITY_TAG,"解析数据结束");
		return gweather;
	}
}
